package com.hdit.guimei.web;

import com.hdit.guimei.domain.User;
import com.hdit.guimei.service.UserService;
import com.hdit.guimei.util.Md5Util;
import com.hdit.guimei.util.OssUtil;
import com.hdit.guimei.util.ResponseData;
import com.hdit.guimei.util.SubStringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @基本功能:
 * @ClassName: UserServlet
 * @Description: TODO
 * @Author: 李嘉明
 * @Date: 2021/1/18 14:33
 * @Version 1.0
 */
@Controller
//@RequestMapping(value = "/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * @Method: userRegister
     * @函数(方法)功能: 用户注册
     * @Author: 李嘉明
     * @params: [user]
     * @Return: int
     * @Exception:
     * @Date: 2021/1/18 16:32
     */
    @RequestMapping(value = "/userRegister")
    @ResponseBody
    public Map userRegister(@RequestBody User user, MultipartFile user_logo) {
        System.out.println(user);
        Map map =new HashMap();
        user.setUser_pwd(Md5Util.md5(user.getUser_pwd()));
        if (user_logo != null&&!user_logo.isEmpty()) {
            String url = new OssUtil().uploadDocument(user_logo, new SubStringUtil().SubFileName(user_logo.getOriginalFilename(),"."));
            user.setUser_head(url);
        }
        System.out.println(user);
        int isAdd = userService.userRegister(user);
        if (isAdd == 1) {
            map.put("code","001");
            map.put("msg","注册成功");
            return map;
        }
        map.put("code","002");
        map.put("msg","出错了！");
        return map;//注册失败
    }

    /**
     * @Method: userLogin
     * @函数(方法)功能: 用户登录
     * @Author: 李嘉明
     * @params: [user_name, user_pwd, session]
     * @Return: int
     * @Exception:
     * @Date: 2021/1/18 16:32
     */
    @RequestMapping(value = "/userLogin")
    @ResponseBody
    public Map userLogin(@RequestBody Map<String ,String> map1, HttpSession session) {
        Map map  = userService.userLogin(map1.get("user_name"), Md5Util.md5(map1.get("user_pwd")));
        return map;
//        System.out.println(map.get("user_name"));
////        System.out.println(user_pwd);
//        System.out.println(isFind);
//        if (isFind == 1) {
////            return 1;//登录成功
//            map1.put("code",001);
//            map1.put("msg","登录成功");
//            map1.put("user",map.get("user_name"));
//            return map1;
//            }
//        if (isFind == -1) {
//            map1.put("msg","密码错误");
////                return -1;//密码错误，登录失败
//            return map1;
//        }
////
//            map1.put("msg","用户名不存在");
//            return map1;


    }

    /**
     * @Method: updateUserPwd
     * @函数(方法)功能: 修改用户密码
     * @Author: 李嘉明
     * @params: [user_name, user_pwd, new_user_pwd]
     * @Return: int
     * @Exception:
     * @Date: 2021/1/19 15:48
     */
    @RequestMapping(value = "/updateUserPwd")
    @ResponseBody
    public int updateUserPwd(String user_name, String user_pwd, String new_user_pwd) {
        int isUpdate = userService.updateUserPwd(user_name, Md5Util.md5(user_pwd), Md5Util.md5(new_user_pwd));
        if (isUpdate == 2) {
            return 2;//修改的用户不存在
        }
        if (isUpdate == -1) {
            return -1;//修改的密码错误
        }
        if (isUpdate == 1) {
            return 1;//修改成功
        }
        return 0;//修改失败
    }
    /**
     * @Method: updateUserInfo
     * @函数(方法)功能: 用户修改信息（包括头像）
     * @Author: 李嘉明
     * @params:  [user, head_logo]
     * @Return: int
     * @Exception:
     * @Date: 2021/1/21 21:21
     */
    @RequestMapping("/updateUserInfo")
    @ResponseBody
    public int updateUserInfo(User user,MultipartFile head_logo){
//        user.setUser_pwd(Md5Util.md5(user.getUser_pwd()));
        if(head_logo!=null&&!head_logo.isEmpty()){
            String head_logoUrl = new OssUtil().uploadDocument(head_logo,new SubStringUtil().SubFileName(head_logo.getOriginalFilename(),"."));
            System.out.println(head_logoUrl);
            user.setUser_head(head_logoUrl);
        }
        return userService.userupdate(user);
    }
    /***
    * @Method 注册时查询用户名是否存在
    * @Author 冰镇小可乐
    * @params  [username]
    * @Return java.util.Map
    * @Exception
    * @Date 2021/1/26 8:55
    */
    @RequestMapping(value = "/findUserName")
    @ResponseBody
    public Map findUserBuUserName( @RequestBody Map<String,String> map){
        System.out.println(map.get("user_name"));
        Map map1=new HashMap();
        map1=userService.findUserByUsername(map.get("user_name"));
        System.out.println(map1);
        return  map1;
    }
    @RequestMapping(value = "/findAllusers")
    @ResponseBody
    public ResponseData<List> findAllUsers(){
      List<User> users = userService.findAllUsers();
        ResponseData<List> res = new ResponseData<>();
        res.setCode(200);
        res.setMess("ok");
        res.setData(users);
        return res;
    }
    @RequestMapping(value = "/updateUserState")
    @ResponseBody
    public ResponseData<Integer> updateUserState(int id ,int user_lock){
        System.out.println(id);
        System.out.println(user_lock);
        int i = userService.updateUserState(id,user_lock);
        ResponseData<Integer> res = new ResponseData<>();
        res.setCode(200);
        res.setMess("ok");
        res.setData(i);
        return res;
    }
}
